package org.example;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Statement;
@Component
public class databaseInit implements CommandLineRunner {
    private final DataSource dataSource;

    public databaseInit(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public void run(String... args){
        /**
         * hsqldb
         */
        try (Connection connection = dataSource.getConnection();
             Statement statement = connection.createStatement()) {
            String sql = "CREATE TABLE IF NOT EXISTS users (" //
                    + "id BIGINT IDENTITY NOT NULL PRIMARY KEY, " //
                    + "email VARCHAR(100) NOT NULL, " //
                    + "password VARCHAR(100) NOT NULL, " //
                    + "name VARCHAR(100) NOT NULL, " //
                    + "createdAt BIGINT NOT NULL, " //
                    + "UNIQUE (email))";
            statement.execute(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }

        /**
         * mysql
         */
//        try (Connection connection = dataSource.getConnection();
//             Statement statement = connection.createStatement()) {
//            String sql = "CREATE TABLE IF NOT EXISTS `users` (\n" +
//                    "  `id` BIGINT NOT NULL,\n" +
//                    "  `name` VARCHAR(100) NOT NULL,\n" +
//                    "  `password` VARCHAR(100) NOT NULL,\n" +
//                    "  `createdAt` BIGINT NOT NULL,\n" +
//                    "  `email` VARCHAR(100) NOT NULL,\n" +
//                    "  PRIMARY KEY (`id`)\n" +
//                    ") ENGINE=INNODB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci";
//            statement.execute(sql);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

    }
}
